﻿using RTW.DAL;
using RTW.OA.Models;
using System.Collections.Generic;
using System.Linq;

namespace RTW.OA.Areas.Common.Servers
{
    public class DocumentBorrowServers
    {
        private readonly IRepository<official_document_borrow> officialDocumentBorrowRepository;

        public DocumentBorrowServers()
        {
            officialDocumentBorrowRepository = new DapperRepository<official_document_borrow>();
        }

        public List<official_document_borrow> GetData()
        {
            var datas = officialDocumentBorrowRepository.GetAllEnumerable();
            return datas.ToList();
        }

        //public List<official_document_borrow> GetData(int docId)
        //{
        //    var datas = officialDocumentBorrowRepository.GetAllEnumerable();
        //    return datas.Where(i => i.DOC_ID == docId).ToList();
        //}

        public IEnumerable<official_document_borrow> GetData(int docId)
        {
            string sql = string.Format("SELECT *,(CASE WHEN [STATUS]=2 AND GETDATE() < DATEADD(dd,[DAYS],[APPROVAL_TIME]) THEN 1 ELSE 0 END) AS 'IsAllowRead' FROM[OADB].[dbo].[official_document_borrow] WHERE DOC_ID = {0}", docId);
            return officialDocumentBorrowRepository.SqlWithParams(sql);
        }

        public IEnumerable<official_document_borrow> GetDataByUserNO(string userNO)
        {
            /*
             * SELECT B.*,(CASE 
	            WHEN B.[STATUS]=2 AND GETDATE()<DATEADD(dd,B.[DAYS],B.[APPROVAL_TIME]) THEN 1 
	            WHEN B.[CREATE_USER_NO] = D.[DRAFT_USER_NO] THEN 1
	            ELSE 0 END) AS IsAllowRead 
                FROM [OADB].[dbo].[official_document_borrow] AS B 
                INNER JOIN [OADB].[dbo].[official_document] AS D ON B.DOC_ID = D.DOC_ID
             */
            string sql = string.Format("SELECT *,(CASE WHEN [STATUS]=2 AND GETDATE() < DATEADD(dd,[DAYS],[APPROVAL_TIME]) THEN 1 ELSE 0 END) AS 'IsAllowRead' FROM[OADB].[dbo].[official_document_borrow] WHERE [CREATE_USER_NO] = '{0}'", userNO);
            return officialDocumentBorrowRepository.SqlWithParams(sql);
        }

        public official_document_borrow GetModelById(int id)
        {
            return officialDocumentBorrowRepository.GetById(id);
        }
		
        internal void Add(official_document_borrow model)
        {
            officialDocumentBorrowRepository.Insert(model);
        }

        internal void Update(official_document_borrow model)
        {
            officialDocumentBorrowRepository.Update(model);
        }
        
        internal void UpdateStatus(official_document_borrow model)
        {
            string sql = string.Format("UPDATE [dbo].[official_document_borrow] SET [STATUS] = '{0}',[APPROVAL_NO]='{1}',[APPROVAL_NAME]='{2}',[APPROVAL_REMARK]='{3}',[APPROVAL_TIME]=GETDATE() WHERE [BORROW_ID] = {4}", model.STATUS,model.APPROVAL_NO,model.APPROVAL_NAME, model.APPROVAL_REMARK, model.BORROW_ID);
            officialDocumentBorrowRepository.SqlWithParams(sql);
        }

        internal void Del(string id)
        {
            officialDocumentBorrowRepository.DeleteByIds(id);
        }

    }
}